package com.starpl.mobile.android.monthunsae.november.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.starpl.mobile.android.monthunsae.november.R;
import com.starpl.mobile.android.monthunsae.november.domain.MonthUnsae;
import com.starpl.mobile.android.monthunsae.november.handler.DailyUnsaeListHandler;
import com.starpl.mobile.android.platform.command.StarplCommand;
import com.starpl.mobile.android.platform.common.DateUtil;
import com.starpl.mobile.android.platform.common.ads.AdBannerOnClickListener;
import com.starpl.mobile.android.platform.common.base.BaseActivity;
import com.starpl.mobile.android.platform.common.base.BaseApplication;
import com.starpl.mobile.android.platform.common.image.ImageDownloader;
import com.starpl.mobile.android.platform.common.network.HttpHandler;
import com.starpl.mobile.android.platform.component.StarplListView;

/**
 * 일별 운세 정보를 보여줍니다.
 * 
 * @author 박우람
 * @since 2011.03.17
 */
public class UnsaeDailyDetailActivity extends BaseActivity implements OnClickListener
{
	private ImageButton mIbRefresh;
	private ImageButton mIbWrite;
	private ImageButton mIbIconDesc;
	private TextView mTvMonth;
	private LinearLayout mLlDailyUnsaeLayout;
	private DailyUnsaeListHandler mListHandler;
	private ImageView mIvBanner;
	private FrameLayout mFlNbizBanner;
	private TextView mTvNbizTitle;
	private TextView mTvNbizDesc;
	private ImageView mIvNbizBanner;
	private LinearLayout mLlCaulyBanner;
	
	/* (non-Javadoc)
	 * @see com.starpl.platform.common.base.BaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.unsae_daily_detail_activity);
		
		mIbRefresh = (ImageButton) findViewById(R.id.unsae_daily_activity_ib_refresh);
		mIbWrite = (ImageButton) findViewById(R.id.unsae_daily_activity_ib_write);
		mIbIconDesc = (ImageButton) findViewById(R.id.unsae_daily_activity_ib_icon_desc);
		mTvMonth = (TextView) findViewById(R.id.unsae_daily_activity_tv_month);
		mLlDailyUnsaeLayout = (LinearLayout) findViewById(R.id.unsae_daily_activity_ll_daily_list);
		mIvBanner = (ImageView) findViewById(R.id.unsae_daily_activity_iv_banner);
		mLlCaulyBanner = (LinearLayout) findViewById(R.id.unsae_daily_detail_activity_cauly_banner);
		
		// NBIZ 광고
		mFlNbizBanner = (FrameLayout) findViewById(R.id.unsae_detail_activity_nbiz_banner);
		mTvNbizTitle = (TextView) findViewById(R.id.nbiz_ad_banner_tv_nbiz_title);
		mTvNbizDesc = (TextView) findViewById(R.id.nbiz_ad_banner_tv_nbiz_description);
		mIvNbizBanner = (ImageView) findViewById(R.id.nbiz_ad_banner_iv_banner);
		
		mIbRefresh.setOnClickListener(this);
		mIbWrite.setOnClickListener(this);
		mIbIconDesc.setOnClickListener(this);
		
		mListHandler = new DailyUnsaeListHandler(this);
		StarplListView listView = new StarplListView(mListHandler);
		mLlDailyUnsaeLayout.addView(listView);
		
		MonthUnsae monthUnsae = (MonthUnsae) BaseApplication.getGlobalPersistent("monthUnsae");
		DateUtil dateUtil = new DateUtil(monthUnsae.getUnsaeDate());
		mTvMonth.setText(dateUtil.toStringByFormat("yyyy년 M월"));
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		
		StarplCommand.getCommonAd(new HttpHandler(this)
		{
			@Override
			public void didSucceed(Object result)
			{
				try
				{
					JSONObject object = (JSONObject) result;
					boolean success = object.getBoolean("success");
					
					// 광고데이터가 정상적으로 날라왔을 경우
					if(success && ! object.isNull("extData3"))
					{
						JSONObject extData3 = object.getJSONObject("extData3");
						int adIdx = extData3.getInt("adIdx");
						
						if(!extData3.isNull("adType"))
						{
							String adType = extData3.getString("adType");
							
							// NBIZ 광고일 경우
							if(adType.equals("ADTC51"))
							{
								JSONObject adData = (JSONObject) extData3.getJSONObject("adData")
															.getJSONObject("zetadoc")
															.getJSONObject("ads")
															.getJSONArray("ad")
															.get(0);
								String title = adData.getString("title");
								String description = adData.getString("description");
								String url = adData.getString("landingUrl");
								String image = "http://shared.starpl.com/images2/ad/mobile/ad_back_android_480_78.png";
								
								mFlNbizBanner.setVisibility(LinearLayout.VISIBLE);
								mFlNbizBanner.setOnClickListener(new AdBannerOnClickListener(url, adIdx));
								mIvBanner.setVisibility(ImageView.GONE);
								mLlCaulyBanner.setVisibility(LinearLayout.GONE);
								mTvNbizTitle.setText(title);
								mTvNbizDesc.setText(description);
								ImageDownloader.download(image, mIvNbizBanner);
							}
							// Cauly 광고일 경우
							else if(adType.equals("ADTC52"))
							{
								AdInfo info = new AdInfo();
								info.initData(BaseApplication.getConfigPersistent("caulyCode"), "CPC", "all", "all", "off", "default", "yes", 90, true);
								
								AdView adView = new AdView(getParentContext());
								adView.setAdListener(new AdListener()
								{
									@Override
									public void onReceiveAd() {}
									
									@Override
									public void onFailedToReceiveAd(boolean arg0) {}
								});
								
								if(mLlCaulyBanner.getChildCount() > 0)
								{
									mLlCaulyBanner.removeAllViews();
								}
								mLlCaulyBanner.addView(adView);
								mLlCaulyBanner.setVisibility(LinearLayout.VISIBLE);
								mFlNbizBanner.setVisibility(LinearLayout.GONE);
								mIvBanner.setVisibility(ImageView.GONE);
							}
							else
							{
								String url = extData3.getString("url");
								String image = extData3.getString("image");
								
								mIvBanner.setVisibility(ImageView.VISIBLE);
								mLlCaulyBanner.setVisibility(LinearLayout.GONE);
								mFlNbizBanner.setVisibility(LinearLayout.GONE);
								mIvBanner.setOnClickListener(new AdBannerOnClickListener(url, adIdx));
								ImageDownloader.download(image, mIvBanner);
							}
						}
					}
				}
				catch(JSONException e)
				{
					LOG.e(e.getMessage());
				}
				
			}
		});
	}

	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View view)
	{
		switch(view.getId())
		{
		case R.id.unsae_daily_activity_ib_icon_desc:
			getNewActivity()
				.setTargetActivity(UnsaeDailyDetailIconActivity.class)
				.show();
			break;
		}
	}
}
